﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of Mp3Cleaner.

    Mp3Cleaner is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace Mp3Edit
{
    public sealed class Mp3OldTag
    {
        //
        private static string[] genre = new string[]
        {
/* 0 */"Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge","Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B","Rap",
/* 16 */"Reggae","Rock","Techno","Industrial","Alternative","Ska","Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient","Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance",
/* 32 */"Classical","Instrumental","Acid","House","Game","Sound Clip","Gospel","Noise","Alt.Rock","Bass","Soul","Punk","Space","Meditative","Instrumental Pop","Instrumental Rock",
/* 48 */"Ethnic","Gothic","Darkwave","Techno-Industrial","Electronic","Pop-Folk","Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta Rap","Top 40","Christian Rap","Pop/Funk","Jungle",
/* 64 */"Native American","Cabaret","New Wave","Psychedelic","Rave","Showtunes","Trailer","Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro","Musical ","Rock & Roll","Hard Rock",
/* 80 */"Folk","Folk/Rock","National Folk","Swing","Fast-Fusion","Bebob","Latin","Revival","Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock","Psychedelic Rock","Symphonic Rock","Slow Rock",
/* 96 */"Big Band","Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson","Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus","Porn Groove","Satire","Slow Jam",
/* 112 */"Club","Tango","Samba","Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle","Duet","Punk Rock","Drum Solo","A Cappella","Euro-House","Dance Hall","Goa","Drum & Bass",
/* 128 */"Club-House","Hardcore","Terror","Indie","BritPop","Negerpunk","Polsk Punk","Beat","Christian Gangsta Rap","Heavy Metal","Black Metal","Crossover","Contemporary Christian","Christian Rock","Merengue","Salsa",
/* 144 */"Thrash Metal","Anime","JPop","Synthpop","","","","","","","","","","","","",
/* 160 */"","","","","","","","","","","","","","","","",
/* 176 */"","","","","","","","","","","","","","","","",
/* 192 */"","","","","","","","","","","","","","","","",
/* 208 */"","","","","","","","","","","","","","","","",
/* 224 */"","","","","","","","","","","","","","","","",
/* 240 */"","","","","","","","","","","","","","","Data",""

        };
        //
        public static bool isOldTag(string fname)
        {
            //
            try
            {
                //
                if (Path.GetExtension(fname).ToUpper() != ".MP3")
                {
                    return false;
                }
                //
                if (!File.Exists(fname))
                {
                    return false;
                }
                //
                byte[] magic = new byte[3];
                //
                FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
                if (fs.Length < 128)
                {
                    // too short
                    return false;
                }
                //
                fs.Seek(-128, SeekOrigin.End);
                fs.Read(magic, 0, 3);
                fs.Close();
                //
                if ((magic[0] == 'T') && (magic[1] == 'A') && (magic[2] == 'G'))
                {
                    return true;
                }
                //
            }
            catch (Exception e)
            {
                return false;
            }
            //
            return false;
        }
        //
        //
        //
        public static bool CutOldTag(string fname)
        {
            bool retVal = true;
            FileStream fs = null;
            //
            try
            {
                //
                if (Path.GetExtension(fname).ToUpper() != ".MP3")
                {
                    return false;
                }
                //
                if (!File.Exists(fname))
                {
                    return false;
                }
                //
                byte[] magic = new byte[3];
                //
                fs = new FileStream(fname, FileMode.Open, FileAccess.ReadWrite);
                if (fs.Length < 128)
                {
                    // too short
                    return false;
                }
                //
                fs.Seek(0, SeekOrigin.Begin);
                fs.Read(magic, 0, 3);
                //
                if (!((magic[0] == 'I') && (magic[1] == 'D') && (magic[2] == '3')))
                {
                    // without ID3
                    fs.Close();
                    return false;
                }
                //
                fs.Seek(-128, SeekOrigin.End);
                fs.Read(magic, 0, 3);
                //
                if ((magic[0] == 'T') && (magic[1] == 'A') && (magic[2] == 'G'))
                {
                    fs.SetLength(fs.Length - 128);
                    // removed!
                }
                //
            }
            catch (Exception e)
            {
                string s = e.Message;
                retVal = false;
            }
            finally
            {
                if (null != fs) fs.Close(); fs = null;
            }
            //
            return retVal;
        }
        //
        //
        public static bool ReadOldTag(string fname, ref string tagTrack, ref string tagTitle, ref string tagArtist, ref string tagAlbum, ref string tagYear, ref string tagGenre)
        {
            //
            //tagTrack = tagTitle = tagArtist = tagAlbum = tagYear = "";
            string __tagTrack="";
            string __tagTitle = "";
            string __tagArtist = "";
            string __tagAlbum = "";
            string __tagYear = "";
            string __tagGenre = "";
            //
            bool retVal = true;
            FileStream fs = null;
            //
            try
            {
                //
                //
                if (Path.GetExtension(fname).ToUpper() != ".MP3")
                {
                    return false;
                }
                //
                //
                //
                if (!Utils.isEmpty(tagTrack) && !Utils.isEmpty(tagTitle) && !Utils.isEmpty(tagArtist) && !Utils.isEmpty(tagAlbum) && !Utils.isEmpty(tagYear))
                {
                    //wszystkie wypełnione, nie trzeba czytać
                    return true;
                }
                //
                //
                if (!File.Exists(fname))
                {
                    return false;
                }
                //
                byte[] magic = new byte[128];
                //
                fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
                if (fs.Length < 128)
                {
                    // too short
                    fs.Close();
                    return false;
                }
                //
                fs.Seek(-128, SeekOrigin.End);
                fs.Read(magic, 0, 128);
                fs.Close(); fs = null;
                //
                if ((magic[0] == 'T') && (magic[1] == 'A') && (magic[2] == 'G'))
                {
                    //
                    for (int i = 3; i < 126; i++)
                    {
                        if ((magic[i] < 32) || (127 == magic[i]) || (255 == magic[i]))
                        {
                            magic[i] = 32;
                        }
                        //
                    }
                    //
                    //tagTitle = "";
                    for (int i = 3; i <= 32; i++)
                    {
                        __tagTitle += (char)magic[i];
                    }
                    __tagTitle = __tagTitle.Trim();
                    if (Utils.isEmpty(tagTitle) && !Utils.isEmpty(__tagTitle)) tagTitle = __tagTitle;
                    //
                    //
                    //tagArtist = "";
                    for (int i = 33; i <= 62; i++)
                    {
                        __tagArtist += (char)magic[i];
                    }
                    __tagArtist = __tagArtist.Trim();
                    if (Utils.isEmpty(tagArtist) && !Utils.isEmpty(__tagArtist)) tagArtist = __tagArtist;
                    //
                    //
                    //tagAlbum = "";
                    for (int i = 63; i <= 92; i++)
                    {
                        __tagAlbum += (char)magic[i];
                    }
                    __tagAlbum = __tagAlbum.Trim();
                    if (Utils.isEmpty(tagAlbum) && !Utils.isEmpty(__tagAlbum)) tagAlbum = __tagAlbum;
                    //
                    //
                    //tagYear = "";
                    for (int i = 93; i <= 96; i++)
                    {
                        __tagYear += (char)magic[i];
                    }
                    __tagYear = __tagYear.Trim();
                    if (Utils.isEmpty(tagYear) && !Utils.isEmpty(__tagYear)) tagYear = __tagYear;
                    //
                    //
                    //tagTrack = "";
                    if ((magic[126] > 0) && (magic[126] < 32))
                    {
                        __tagTrack = magic[126].ToString();
                    }
                    if (Utils.isTrackNumberEmpty(tagTrack) && !Utils.isTrackNumberEmpty(__tagTrack)) tagTrack = __tagTrack;
                    //
                    //
                    //__tagGenre = "";
                    __tagGenre = genre[magic[127]];
                    if (Utils.isTrackNumberEmpty(tagGenre) && !Utils.isTrackNumberEmpty(__tagGenre)) tagGenre = __tagGenre;
                    //
                }
                //
            }
            catch (Exception e)
            {
                string s = e.Message;
                retVal = false;
            }
            finally
            {
                if (null != fs) fs.Close(); fs = null;
            }
            return retVal;
        }
        //
        //
    }//class
}
